﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using System.ServiceModel;

namespace DAL
{
    /// <summary>
    /// Use EntityFramework to Implement UserRepository
    /// </summary>
    public class UserRepository : IUserRepository
    {
        WorkShopEntities dbContext = new WorkShopEntities();
        /// <summary>
        /// Save a New User to Database
        /// </summary>
        /// <param name="User"></param>
        /// <returns></returns>
        public bool AddNewUser(BLL.User User)
        {
            //if (User == null)
            //{
            //    throw new FaultException("Use object is null ");
            //}

            DAL.User user = DAL.Translator.UserTranslator.TranslateBusinessToEntity(User);
            DAL.Role role = (from Role a in dbContext.Role where a.RoleId == user.RoleId && a.Status.StatusId == 1 select a).SingleOrDefault<Role>();
            if (role != null)
            {
                //try
                //{
                    dbContext.AddToUser(user);
                    dbContext.SaveChanges();
                //}
                //catch (Exception ex)
                //{
                //    throw new FaultException(ex.ToString());
                //}
                return true;
            }
            else
            {
                throw new FaultException("This role can not use right now. ");
            }
        }


        /// <summary>
        /// Delete User from Database
        /// </summary>
        /// <param name="UserId"></param>
        public void DeleteUser(int UserId)
        {
            DAL.User user = (from User u in dbContext.User where u.UserId == UserId select u).SingleOrDefault<User>();
            //List<DAL.User> userList = new List<DAL.User>();
            //userList = (from User u in dbContext.User where u.UserId == user.UserId select u).ToList();
            if (user != null)
            {
                dbContext.User.DeleteObject(user);
                dbContext.SaveChanges();
            }
            else
            {
                throw new FaultException("This user has been deleted.");
            }  
        }


        /// <summary>
        /// Update User to Database
        /// </summary>
        /// <param name="User"></param>
        public void UpdateUser(BLL.User User)
        {
            DAL.User user = DAL.Translator.UserTranslator.TranslateBusinessToEntity(User);
            List<DAL.User> userList = new List<DAL.User>();
            userList = (from User u in dbContext.User where u.UserId == user.UserId select u).ToList();
            DAL.Role role = (from Role r in dbContext.Role where r.RoleId == user.RoleId && r.Status.StatusId == 1 select r).SingleOrDefault<Role>();

            if (role != null && userList.Count != 0 && userList != null)
            {
                user = (from User u in dbContext.User where u.UserId == user.UserId select u).SingleOrDefault<User>();
                user.FirstName = User.FirstName;
                user.LastName = User.LastName;
                user.DepartmentId = User.Department.DepartmentId;
                user.RoleId = User.Role.RoleId;
                dbContext.SaveChanges();
            }
            else if (role == null && userList.Count != 0 && userList != null)
            {
                throw new FaultException("This Role can not use right now.", new FaultCode("Role Deleted"));
            }
            else
            {
                throw new FaultException("Failed!Please try again later.", new FaultCode("User Deleted"));
            }

        }


        /// <summary>
        /// Get All User
        /// </summary>
        /// <returns></returns>
        public List<BLL.User> GetAllUser()
        {
            List<BLL.User> listBLL = new List<BLL.User>();
            List<DAL.User> listDAL = (from u in dbContext.User join d in dbContext.Department on u.Department.DepartmentId equals d.DepartmentId join r in dbContext.Role on u.Role.RoleId equals r.RoleId select u).ToList<User>();
            foreach (DAL.User userDAL in listDAL)
            {
                BLL.User userBLL = DAL.Translator.UserTranslator.TranslateEntityToBusiness(userDAL);
                listBLL.Add(userBLL);
            }
            return listBLL;
        }


        /// <summary>
        /// Get User by UserId
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public BLL.User GetUserById(int UserId)
        {
            DAL.User userDAL = (from User u in dbContext.User where u.UserId == UserId select u).SingleOrDefault<User>();
            if (userDAL != null)
            {
                BLL.User userBLL = Translator.UserTranslator.TranslateEntityToBusiness(userDAL);
                return userBLL;
            }
            else
            {
                throw new FaultException("This user has been deleted.");
            }
        }

        /// <summary>
        /// Search User by Complex Conditions
        /// </summary>
        /// <param name="departmentid"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public List<BLL.User> SearchUser(int departmentid, string firstname, string lastname)
        {
            using (WorkShopEntities db = new WorkShopEntities())
            {
                List<BLL.User> listBLL = new List<BLL.User>();
                List<DAL.View_UserInfo> listDAL = db.SearchUser(departmentid,firstname,lastname).ToList();
                foreach (DAL.View_UserInfo userDAL in listDAL)
                {
                    BLL.User userBLL = DAL.Translator.UserTranslator.TranslateEntityViewToBusiness(userDAL);
                    listBLL.Add(userBLL);
                }
                return listBLL;
            }
        }

        /// <summary>
        /// Get All Department
        /// </summary>
        /// <returns></returns>
        public List<BLL.Department> GetAllDepartment()
        {
            List<BLL.Department> listBLL = new List<BLL.Department>();
            List<DAL.Department> listDAL = (from Department d in dbContext.Department select d).ToList<Department>();
            foreach (DAL.Department deptDAL in listDAL)
            {
                BLL.Department deptBLL = DAL.Translator.DepartmentTranslator.TranslateEntityToBusiness(deptDAL);
                listBLL.Add(deptBLL);
            }
            return listBLL;
        }
    }
}






